<template>
  <Form :model="formItem" :rules="ruleInline" :label-width="120" ref="scheduler_form">
    <Alert show-icon>
      基本信息
      <Icon type="ios-lightbulb-outline" slot="icon"></Icon>
    </Alert>
    <FormItem label="定时任务说明" prop="comment" style="margin-top: 20px;">
      <Input v-model="formItem.comment" placeholder="请输入定时任务说明..."></Input>
    </FormItem>
    <FormItem label="执行频率" prop="interval" style="margin-top: 20px;">
      <Input v-model="formItem.interval" placeholder="请输入执行频率..."></Input>
    </FormItem>
    <FormItem label="执行方法" prop="execute_method" style="margin-top: 20px;">
      <Input v-model="formItem.execute_method" placeholder="请输入执行方法..."></Input>
    </FormItem>
    <FormItem>
      <Button type="primary" @click="handleSubmit">提交</Button>
      <Button type="primary" @click="handleReturn" style="margin-left: 8px">返回</Button>
      <Button type="primary" @click="handleReset" style="margin-left: 8px">清空</Button>
    </FormItem>
  </Form>
</template>
<script>
  export default {
    data() {
      return {
        formItem: {
          comment: "",
          status: "0",
          interval: "",
          execute_method: "",
        },
        ruleInline: {
          comment: [
            {required: true, message: '此项为必添项!', trigger: 'blur'},
            {max: 255, message: '长度不可以超过255!', trigger: 'blur'}
          ],
          interval: [
            {required: true, message: '此项为必添项!', trigger: 'blur'},
            {max: 255, message: '长度不可以超过255!', trigger: 'blur'}
          ],
          execute_method: [
            {required: true, message: '此项为必添项!', trigger: 'blur'},
            {max: 255, message: '长度不可以超过255!', trigger: 'blur'}
          ],
        }
      }
    },
    beforeCreate: function () {
      this.$Spin.show();
    },
    created: function () {
      const id = this.params.id;
      if ("" != id) {//这个说明是修改，要发请求获得更有的数据
        this.$http.get(this.apiUrl() + 'scheduler/get/' + id, {}, {
          emulateJSON: true
        }).then((response) => {
          if (response && response.body.success) {
            this.formItem.comment = response.body.scheduler.comment;
            this.formItem.status = response.body.scheduler.status;
            this.formItem.interval = response.body.scheduler.interval;
            this.formItem.execute_method = response.body.scheduler.execute_method;
          } else {
            if (response.body.msg === 'session-timeout') {
              window.location.href = "/";
            } else {
              this.$Notice.error({title: "错误", desc: response.body.msg});
            }
          }
          this.$Spin.hide();
        });
      } else {
        this.$Spin.hide();
      }
    },
    methods: {
      handleSubmit: function () {
        this.$refs['scheduler_form'].validate((valid) => {
          if (valid) {
            this.$http.post(this.apiUrl() + 'scheduler/save/', {
              id: this.params.id,
              comment: this.formItem.comment,
              status: this.formItem.status,
              interval: this.formItem.interval,
              execute_method: this.formItem.execute_method,
              group_id: this.params.group_id
            }, {
              emulateJSON: true
            }).then((response) => {
              if (response.body.success) {
                this.$Message.success(response.body.msg);
                this.$emit('childClickMenu', '系统管理', '定时任务管理', 'scheduler_list');
              } else {
                this.$Notice.error({title: '错误', desc: response.body.msg});
              }
            });
          } else {
            this.$Notice.error({title: '错误', desc: '请输入完事信息后进行提交!'});
          }
        })
      },
      handleReturn: function () {
        this.$emit('childClickMenu', '系统管理', '定时任务管理', 'scheduler_list');
      },
      handleReset: function () {
        this.$refs['scheduler_form'].resetFields();
      }
    },
    props: ['params']
  }
</script>
<style scoped>
  page_tools {
    float: right;
    padding-top: 10px;
    padding-bottom: 10px;
  }
</style>
